﻿CREATE PROCEDURE [dbo].[proc_Order_Change_ProjectItem_Create]
	(
		@OId uniqueidentifier,
		@CId int,
		@ProductId int,
		@ProductName nvarchar(50),
		@SupplierId int,
		@SupplierName nvarchar(50),
		@CostPrice decimal(18,2),
		@SalePrice decimal(18,2),
		@Description nvarchar(200),
		@TypeId int,
		@TypeName nvarchar(50),
		@IsFinish bit,
		@CreateDate datetime,
		@CreateUser nvarchar(50)
	)
AS
begin

	--订单数据
	Declare @CompanyId int, @CusId uniqueidentifier , @CusName nvarchar(50) , @Salesman nvarchar(50), @SalesmanAccount nvarchar(50)
	Select
	@CompanyId=CompanyId,
	@Salesman=Salesman,
	@SalesmanAccount=SalesmanAccount
	From [Order] Where OId=@OId
	
	--项目执行数据
	Declare @ExecPerson int , @ExecDepartmentId int
	Select @ExecPerson=ISNULL(UserName,'0'),@ExecDepartmentId=ISNULL(DepartmentId,'0') From ProjectInterfaceSet Where ProductId=@ProductId and CompanyId=@CompanyId
	
	
--	EXEC @CompanyId=proc_Company_GetID @CreateUser,@CompanyId
	
	--
	Declare @SCateOneId int, @SCateTwoId int, @SubjectId int
	Select @SCateOneId=SCateOneId,@SCateTwoId=SCateTwoId,@SubjectId=SubjectId From [Product] Where Id=@ProductId

	Update [Order_Change] Set flag=1 Where OId=@OId And Id=@CId And flag=0
	Insert Into Order_Change_Project
		(
			OId,
			CId,
			ProductId,
			ProductName,
			SupplierId,
			SupplierName,
			CostPrice,
			SalePrice,
			Description,
			TypeId,
			TypeName,
			IsFinish,
			CompanyId,
			Salesman,
			SalesmanAccount,
			CreateUser,
			ExecPerson,
			ExecDepartmentId,
			FinanceAudit,
			SCateOneId,
			SCateTwoId,
			SubjectId
		)
	Values
		(
			@OId,
			@CId,
			@ProductId,
			@ProductName,
			@SupplierId,
			@SupplierName,
			@CostPrice,
			@SalePrice,
			@Description,
			@TypeId,
			@TypeName,
			@IsFinish,
			@CompanyId,
			@Salesman,
			@SalesmanAccount,
			@CreateUser,
			@ExecPerson,
			@ExecDepartmentId,
			1,
			@SCateOneId,
			@SCateTwoId,
			@SubjectId
		)
end
	RETURN